home *** CD-ROM | disk | FTP | other *** search
- Path: locutus.rchland.ibm.com!usenet
- From: pstaite@vnet.ibm.com
- Newsgroups: comp.lang.c++
- Subject: Re: overloading []
- Date: 18 Jan 1996 14:41:58 GMT
- Organization: IBM OS/2 Device Driver Development Rochester, MN
- Message-ID: <4dlm7m$on9@locutus.rchland.ibm.com>
- References: <4dgjbl$6i3@news1.goodnet.com> <4dh86s$bfi@locutus.rchland.ibm.com> <ENNO.96Jan17200900@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- Reply-To: pstaite@vnet.ibm.com
- NNTP-Posting-Host: warpone.rchland.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <ENNO.96Jan17200900@kitz.inferenzsysteme.informatik.th-darmstadt.de>, enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner) writes:
- >In article <4di9v9$8k6@news.bridge.net> David Byrden <100101.2547@compuserve.com> writes:
- >
- > >> No, you could do:
- >
- > >> foo& f( *new foo );
- > >> later on:
- >
- > >> delete &f;
- >
- >
- > Phil, I believe the C++ standards committee have decided to play safe by
- > declaring this kind of code as "ill-formed". In other words, a 'perfect'
- > C++ compiler need not behave as you want here.
-
- Hmm, I'll have to look through the DWP and see if this will become
- illegal, I hope not.
-
- Actually, if I were writing the code to use I wouldn't do it quite so
- briefly. When I post code to the net I usually don't include much (any)
- error checking just to keep it short. If I were really going to do
- something like this I'd code:
-
- foo* p( new foo );
- if( ! p ) {
- // do something, probably error
- // out of the function
- }
- foo& f( *p );
- f[ 0 ] = // etc. etc.
-
- delete p;
- // end of scope
-
-
- Note, you have to be careful where you do the delete p since you're
- pulling the rug out from underneath the ref f. Also, if your compiler
- will throw an exception from new instead of returning NULL that makes
- life easier.
-
- If the function were short and I was only referencing the foo a couple
- of times I wouldn't bother with the reference, I'd just use (*p)[]. The
- ref is just there for readability, but it's setup requires some code
- that detracts from readability (IMHO). So I'd have to be looking at a
- lot of (*p)[] constructs before I'd sign up for the ref.
-
-
- Phil Staite, team OS/2
- internet: pstaite@vnet.ibm.com internal: pstaite@rchland
-
-